﻿/**
 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckeditor.com/license
 */

/**
 * @fileOverview Horizontal Page Break
 */

'use strict';

( function() {
	CKEDITOR.plugins.add( 'custom', {
		requires: 'dialog',
		lang: 'zh-cn', // %REMOVE_LINE_CORE%

		init: function( editor ) {
			// Register the command.

            editor.addCommand( 'upload', new CKEDITOR.dialogCommand( 'uploadCmd' ) );
			// Register the toolbar button.
			editor.ui.addButton && editor.ui.addButton( 'Image', {
				label: editor.lang.custom.toolbar,
				command: 'upload',
				toolbar:'insert,0'
			} );

		}

	} );
    CKEDITOR.dialog.add( 'uploadCmd', function ( editor ) {
        return {
            title: "插入图片",
            minWidth: "660px",
            minHeight:"400px",
            contents: [{
                id: "tab1",
                label: "",
                title: "",
                expand: true,
                padding: 0,
                elements: [{
                    id:'images-frame',
                    type: "html",
                    style: "width:400px;height:400px",
                    html: '<iframe id="uploadFrame" src="'+editor.plugins.custom.path+'/image.html?v=' +new Date().getSeconds() + '" frameborder="0"></iframe>'
                }]
            }],
            // when the dialog ended width ensure,"onOK" will be executed.
            onOk: function() {

                var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
                var isIE = (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 )|| userAgent.indexOf("Trident") > -1;

                if (isIE) {
                    iframe = $(window.uploadFrame.document);
                }else{
                    var iframe = $(window.uploadFrame.contentWindow.document);
                }
                var link = iframe.find('input[name="link"]').val();
                var width = iframe.find('input[name="width"]').val();
                if(width == 'undefined' || width.length == 0){
                    width = 'auto';
                }
                var height = iframe.find('input[name="height"]').val();
                if(height == 'undefined' || height.length == 0){
                    height = 'auto';
                }
                var image = editor.document.createElement( 'img', {
                    attributes: {src:link,width:width,height:height}
                } );
                editor.insertElement( image );
            }
        }
    });
    // TODO Much probably there's no need to expose this object as public object.
} )();
